#include "MutaEvent.h"
#include <QGraphicsSceneMouseEvent>
#include <QCursor>
#include "math.h"
#include <QRadialGradient>
#include <QGraphicsBlurEffect>
#include <QRectF>
#include <QPainter>
#include "Global.h"

MutaEvent::MutaEvent(QString objectName,QString owner, qreal x, qreal y,int layer)
    : QGraphicsRectItem( 1, 1, Muta::BEAT_PIXELS -1, 14 ),
      MutaObject(objectName,Muta::Event,owner),
      layer(layer)

{
    if(Muta::ONLINE) {
        ((QGraphicsRectItem*)this)->setPos(x,y);
    } else {
        setPos(QPointF(x,y));
    }
    setColor();
    setCacheMode( QGraphicsItem::ItemCoordinateCache );
    setFlag( QGraphicsItem::ItemIsSelectable, true );
    setFlag( QGraphicsItem::ItemIsMovable, true );
    setFlag(QGraphicsItem::ItemSendsGeometryChanges);
}

MutaEvent::MutaEvent(MutaEvent* copy):
    QGraphicsRectItem( 1, 1, Muta::BEAT_PIXELS -1, 14 ),
    MutaObject(copy->getObjectID(),Muta::Event,copy->getOwner()),
    layer(copy->getLayer())
{
    setPos(QPointF(copy->x(),copy->y()));
    changeLength(copy->rect().width());
    setColor();
    setCacheMode( QGraphicsItem::ItemCoordinateCache );
    setFlag( QGraphicsItem::ItemIsSelectable, true );
    setFlag( QGraphicsItem::ItemIsMovable, true );
}

int MutaEvent::type() const
{
    return mutaType;
}

void MutaEvent::setX(qreal x)
{
    qreal initX = this->x();
    QGraphicsRectItem::setX(Muta::quantizeD(x,Muta::BEAT_PIXELS));

    //if(initX!=x) {
        //emit posChanged(objectID,pos());
    //}
}

void MutaEvent::setY(qreal y)
{
    qreal initY = this->y();
    QGraphicsRectItem::setY(Muta::quantizeD(y,15));

    //if(initY!=y) {
        //emit posChanged(objectID,pos());
    //}
}

void MutaEvent::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
    QGraphicsRectItem::mouseMoveEvent(event);
    setPos(pos());
}

void MutaEvent::setPos(const QPointF &pos)
{
    //qreal initX = this->x();
    //qreal initY = this->y();
    QGraphicsRectItem::setPos(Muta::quantizePointD(pos,Muta::BEAT_PIXELS,15));
    //qDebug() << initX << x();
    //qDebug() << initY << y();
    //if((initX!=x()) || (initY!=y())) {
        //qDebug() << "POS CHANGED!";
        //emit posChanged(objectID,pos);
    //}
}

void MutaEvent::moveBy(qreal dx, qreal dy)
{
    setPos(this->pos() + QPointF(dx,dy));
}

void MutaEvent::changeLength(QPointF point)
{
    rWidth = Muta::quantize(point.x() - this->x(),Muta::BEAT_PIXELS)-1;
    setRect(1,1,rWidth,14);
}
void MutaEvent::changeLength(float length)
{
    setRect(1,1,length,14);
}

void MutaEvent::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen;
    QColor outlineColor = QColor(255,0,0,255);
    QColor outlineColor2 = QColor(0,0,0,255);
    switch(layer) {
        case 0:
            //fillColor = QColor(227,244,247,255);
            //firstCPointColor = QColor(140,204,240,255);
            outlineColor = QColor(64,187,236,255);
            break;
        case 1:
            //fillColor = QColor(216,191,215,255);
            //firstCPointColor = QColor(194,118,177,255);
            outlineColor = QColor(124,40,125,255);
            break;
        case 2:
            //fillColor = QColor(255,153,153);
            //firstCPointColor = QColor(242, 99, 75,255);
            outlineColor = QColor(237, 31,36,255);
            break;
        case 3:
            //fillColor = QColor(238,232,169,255);
            //firstCPointColor = QColor(179,210,53,255);
            outlineColor = QColor(86,107,48,255);
            break;
        case 4:
            //fillColor = QColor(255,204,153,255);
            //firstCPointColor = QColor(210,106,40,255);
            outlineColor = QColor(139,70,31,255);
            break;
    }

    pen.setWidth(6);
    pen.setColor(outlineColor);
    painter->setPen(pen);
    painter->drawLine(2,2,2,14);
    pen.setColor(outlineColor2);
    painter->setPen(pen);
    painter->drawLine(rect().width(),0,rect().width(),14);
    QGraphicsRectItem::paint(painter,option,widget);
}

void MutaEvent::setColor()
{
        QColor fillColor;
        QColor outlineColor;
        switch(layer) {
            case 0:
                fillColor = QColor(227,244,247,180);
                //firstCPointColor = QColor(140,204,240,255);
                outlineColor = QColor(64,187,236,255);
                break;
            case 1:
                fillColor = QColor(216,191,215,180);
                //firstCPointColor = QColor(194,118,177,255);
                outlineColor = QColor(124,40,125,255);
                break;
            case 2:
                fillColor = QColor(255,153,153,180);
                //firstCPointColor = QColor(242, 99, 75,255);
                outlineColor = QColor(237, 31,36,255);
                break;
            case 3:
                fillColor = QColor(238,232,169,180);
                //firstCPointColor = QColor(179,210,53,255);
                outlineColor = QColor(86,107,48,255);
                break;
            case 4:
                fillColor = QColor(255,204,153,180);
                //firstCPointColor = QColor(210,106,40,255);
                outlineColor = QColor(139,70,31,255);
                break;
        }



    QBrush brush;
    brush.setColor(fillColor);
    brush.setStyle(Qt::SolidPattern);

    QPen pen;
    this->setBrush(brush);
    pen.setWidth(1);
    pen.setColor( outlineColor );
    pen.setJoinStyle(Qt::RoundJoin);
    pen.setCapStyle(Qt::RoundCap);
    setPen( pen );
}

int MutaEvent::getLayer()
{
    return layer;
}

QStringList MutaEvent::getProperties()
{
    QStringList properties;
    properties << "me" << owner << QString::number(x()) << QString::number(y()) << QString::number(layer) << QString::number(this->rect().width());
    return properties;
}

QVariant MutaEvent::itemChange(GraphicsItemChange change, const QVariant &value) {
    QVariant variant = QGraphicsRectItem::itemChange(change, value);

    if(change == QGraphicsItem::ItemPositionChange || change == QGraphicsItem::ItemPositionHasChanged)
    {
        QPointF initPos = this->pos();
        variant.setValue(Muta::quantizePoint(variant.toPointF(),Muta::BEAT_PIXELS,15));
    }

    return variant;
}

void MutaEvent::remoteSetX(float x)
{
    ((QGraphicsRectItem*)this)->setX(Muta::quantizeD(x,Muta::BEAT_PIXELS));
    //qDebug() << "OSCthulhu set x: " << x;
}

void MutaEvent::remoteSetY(float y)
{
    ((QGraphicsRectItem*)this)->setY(Muta::quantizeD(y,15));
    //qDebug() << "OSCthulhu set y: " << y;
}

void MutaEvent::forceOSCthulhuUpdate()
{
    emit posChanged(objectID,pos());
}
